package cn.snowsoft.iot.module.cps.mqtt;

import cn.snowsoft.iot.module.cps.controller.admin.equipment.vo.EquipmentPage;
import cn.snowsoft.iot.module.cps.dal.dataobject.equipment.EquipmentDO;
import cn.snowsoft.iot.module.cps.dal.dataobject.product.ProductDO;
import okhttp3.RequestBody;

import java.io.IOException;
import okhttp3.*;

public class Authentication {

    public static void insertAuthentication(EquipmentDO equipment) throws Exception{
        OkHttpClient client = new OkHttpClient();
        // 创建请求体
        String json = "{\"user_id\":\"" + equipment.getUserAccount() + "\",\"password\":\"" + equipment.getUserPassword() + "\"}";
        RequestBody body = RequestBody.create(json, MediaType.parse("application/json"));
        // 创建请求
        Request request = new Request.Builder()
                .url(EmqxConnect.INSTALL_URL)
                .post(body)
                .header("Content-Type", "application/json")
                .addHeader("Authorization",Credentials.basic(EmqxConnect.apiKey , EmqxConnect.apiSecret))
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            }
            System.out.println(response.body().string());
        }
    }


    public static void insertAuthenticationByProduct(ProductDO product) throws Exception{
        OkHttpClient client = new OkHttpClient();
        String json = "{\"user_id\":\"" + product.getUserAccount() + "\",\"password\":\"" + product.getUserPassword() + "\"}";
        RequestBody body = RequestBody.create(json, MediaType.parse("application/json"));
        Request request = new Request.Builder()
                .url(EmqxConnect.INSTALL_URL)
                .post(body)
                .header("Content-Type", "application/json")
                .addHeader("Authorization",Credentials.basic(EmqxConnect.apiKey , EmqxConnect.apiSecret))
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            }
            System.out.println(response.body().string());
        }
    }
    /**
     * 删除认证器
     */
    public static void removeAuthentication(EquipmentPage equipment) throws Exception{
        OkHttpClient client = new OkHttpClient();
        // 创建请求
        Request request = new Request.Builder()
                .url(EmqxConnect.DELETE_URL+ "/" + equipment.getUserAccount())
                .delete()
                .header("Content-Type", "application/json")
                .addHeader("Authorization",Credentials.basic(EmqxConnect.apiKey , EmqxConnect.apiSecret))
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            }
            System.out.println(response.body().string());
        }

    }
}